﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Meqs.Logic.Core
{
    /// <summary>
    /// 数据库异常类
    /// dzf
    /// </summary>
    public class DBServiceException:Exception
    {
        //提示语言
        //private string StrText=string.Empty;
        private string StrMessage = string.Empty;
        /// <summary>
        /// 调用堆栈
        /// </summary>
        private string StrStackTrace = string.Empty;
        /// <summary>
        /// 可能具有的sql语句
        /// </summary>
        private string Strsql = string.Empty;
        /// <summary>
        /// 原异常
        /// </summary>
        private Exception ex = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="e"></param>
        public DBServiceException(Exception e)
        {
            ex = e;
        }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="e"></param>
        public DBServiceException(string strText,Exception e)
        {
            StrMessage = strText;
            ex = e;
        }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="StrMessage"></param>
        /// <param name="StrStackTrace"></param>
        /// <param name="Strsql"></param>
        /// <param name="e"></param>
        public DBServiceException(string StrMessage, string StrStackTrace, string Strsql,Exception e)
        {
            this.StrMessage = StrMessage;
            this.StrStackTrace = StrStackTrace;
            this.Strsql = Strsql;
            ex = e;
        }
        /// <summary>
        /// 获取原异常
        /// </summary>
        public Exception BaseException
        {
            get 
            {
                return ex;
            }
        }
        /// <summary>
        /// 获取异常信息
        /// </summary>
        public override string Message
        {
            get
            {
                return StrMessage;
            }
        }
        /// <summary>
        /// 显示错误信息并且提示
        /// </summary>
        /// <returns></returns>
        public string ShowError()
        {
            log4net.ILog _log = log4net.LogManager.GetLogger(typeof(MsSQL));
            if (BaseException != null)
                _log.Error(StrMessage + "\r\n" + Strsql, BaseException);
            else
                _log.Error(StrMessage);
            
            //System.Windows.Forms.MessageBox.Show(StrText, "数据库异常",
            //    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
            return StrMessage;

        }
        /// <summary>
        /// 显示错误信息并且提示
        /// </summary>
        /// <param name="sender">调用类</param>
        /// <returns></returns>
        public string ShowError(object sender)
        {
            log4net.ILog _log =  log4net.LogManager.GetLogger(typeof(MsSQL));
            if (sender != null)
            {
                _log = log4net.LogManager.GetLogger(sender.GetType());
            }
            if (BaseException != null)
                _log.Error(StrMessage + "\r\n" + Strsql, BaseException);
            else
                _log.Error(StrMessage);
            
            //System.Windows.Forms.MessageBox.Show(StrText, "数据库异常",
            //    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
            return StrMessage;

        }
    }
}